package com.dd.controller;


import com.dd.pojo.User;
import com.dd.pojo.Vip;
import com.dd.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/finduser")
    public String selectuser(String uname,String upwd){
        User user = userService.selectuser(uname, upwd);
        if (user != null){
            return "1";
        }else {
            return "0";
        }
    }

    @RequestMapping("/adduser")
    public String adduser(User user){
        userService.adduser(user);
        return "1";
    }

    @RequestMapping("/selectByname")
    public String selectByname(String uname) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user = userService.selectByname(uname);
        String str = mapper.writeValueAsString(user);
        String jso = "["+str +"]";
        return jso;
    }

    @RequestMapping("/updateuser")
    public String updateuser(User user){
        userService.updateuser(user);
        return "1";
    }

    @RequestMapping("/selectusername")
    public String selectusername() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<String> s = userService.selectusername();
        String str = mapper.writeValueAsString(s);
        return str;
    }


    @RequestMapping("/selectname")
    public String selectname(String name) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user = userService.selectname(name);
        String str = mapper.writeValueAsString(user);
        String jso = "["+str +"]";
        return jso;
    }
}
